﻿namespace FiftyOne.Foundation.Mobile.Detection.Wurfl.Configuration
{
    using System;
    using System.Configuration;
    using System.Reflection;

    public sealed class PatchesCollection : ConfigurationElementCollection
    {
        public void Add(PatchConfigElement wurflPatch)
        {
            if (wurflPatch == null)
            {
                throw new ArgumentNullException("wurflPatch");
            }
            this.BaseAdd(wurflPatch);
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            base.BaseAdd(element, false);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new PatchConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((PatchConfigElement) element).Name;
        }

        public int IndexOf(PatchConfigElement wurflPatch)
        {
            if (wurflPatch == null)
            {
                throw new ArgumentNullException("wurflPatch");
            }
            return base.BaseIndexOf(wurflPatch);
        }

        public void Remove(PatchConfigElement wurflPatch)
        {
            if (wurflPatch == null)
            {
                throw new ArgumentNullException("wurflPatch");
            }
            if (base.BaseIndexOf(wurflPatch) >= 0)
            {
                base.BaseRemove(wurflPatch.Name);
            }
        }

        public void Remove(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            base.BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            if ((index < 0) || (index > (base.Count - 1)))
            {
                throw new ArgumentOutOfRangeException("index");
            }
            base.BaseRemoveAt(index);
        }

        public PatchConfigElement this[string name]
        {
            get
            {
                return (PatchConfigElement) base.BaseGet(name);
            }
        }

        public PatchConfigElement this[int index]
        {
            get
            {
                return (PatchConfigElement) base.BaseGet(index);
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
    }
}

